package com.xxadmin.usercenter.controller;

import com.github.pagehelper.Page;
import com.xxadmin.common.dto.PageInfoDTO;
import com.xxadmin.common.util.page.PageUtils;
import com.xxadmin.common.validate.ValidGroup1;
import com.xxadmin.common.validate.ValidGroup2;
import com.xxadmin.common.vo.ResponseModelBuilder;
import com.xxadmin.common.util.clone.CopyUtils;
import com.xxadmin.common.vo.PageInfoVO;
import com.xxadmin.common.vo.ResponseModel;
import com.xxadmin.common.web.BaseController;
import com.xxadmin.usercenter.domain.ChannelDetail;
import com.xxadmin.usercenter.dto.ChannelDetailDTO;
import com.xxadmin.usercenter.dto.ChannelDetailQueryDTO;
import com.xxadmin.usercenter.service.ChannelDetailService;
import com.xxadmin.usercenter.vo.request.ChannelDetailQueryVO;
import com.xxadmin.usercenter.vo.request.ChannelDetailVO;
import com.xxadmin.usercenter.vo.response.ChannelDetailResponseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 渠道 Controller
 *
 * @author:chenming
 * @date:2018/8/29
 */
@RestController
@RequestMapping("/channelDetail")
@Validated
public class ChannelDetailController{

    @Autowired
    private ChannelDetailService channelDetailService;

    @GetMapping("/listChannelDetail")
    public ResponseModel<PageInfoVO> listChannelDetail(ChannelDetailQueryVO queryVO){

        ChannelDetailQueryDTO queryDTO = CopyUtils.copyBean(queryVO,ChannelDetailQueryDTO.class);

        PageInfoDTO<ChannelDetailDTO> pageInfoDTO = channelDetailService.listChannelDetail(queryDTO);

        PageInfoVO<ChannelDetailVO> pageInfoVO = PageUtils.getPageInfoVO(pageInfoDTO,ChannelDetailVO.class);

        return ResponseModelBuilder.genericSuccessResponseModal(pageInfoVO);
    }

    @PostMapping("/add")
    public ResponseModel addChannelDetail(@RequestBody @Validated(ValidGroup1.class) ChannelDetailVO channelDetailVO){

        ChannelDetailDTO channelDetail = CopyUtils.copyBean(channelDetailVO,ChannelDetailDTO.class);

        channelDetailService.addChannelDetail(channelDetail);

        return ResponseModelBuilder.genericSuccessResponseModal();
    }

    @GetMapping("/get/{id}")
    public ResponseModel<ChannelDetailResponseVO> getChannelDetail( @PathVariable("id") Integer id){

        ChannelDetailDTO channelDetail = channelDetailService.getChannelDetailById(id);

        ChannelDetailResponseVO vo = CopyUtils.copyBean(channelDetail,ChannelDetailResponseVO.class);

        return ResponseModelBuilder.genericSuccessResponseModal(vo);
    }

    @PutMapping("/update")
    public ResponseModel updateChannelDetail(@RequestBody @Validated(ValidGroup2.class) ChannelDetailVO channelDetailVO){
        ChannelDetailDTO channelDetail = CopyUtils.copyBean(channelDetailVO,ChannelDetailDTO.class);

        channelDetailService.updateChannelDetail(channelDetail);

        return ResponseModelBuilder.genericSuccessResponseModal();
    }
}
